package com.kxmall.web.controller.newtimes;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.kxmall.common.annotation.Log;
import com.kxmall.common.annotation.RepeatSubmit;
import com.kxmall.common.core.controller.BaseController;
import com.kxmall.common.core.domain.PageQuery;
import com.kxmall.common.core.domain.R;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.core.validate.AddGroup;
import com.kxmall.common.core.validate.EditGroup;
import com.kxmall.common.enums.BusinessType;
import com.kxmall.common.utils.poi.ExcelUtil;
import com.kxmall.newtimes.domain.bo.KxNewTimesBo;
import com.kxmall.newtimes.domain.vo.KxNewTimesVo;
import com.kxmall.web.controller.newtimes.service.IKxNewTimesService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 新鲜时报
 *
 * @author kxmall
 * @date 2023-10-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/newtimes/newTimes")
public class KxNewTimesController extends BaseController {

    private final IKxNewTimesService iKxNewTimesService;

    /**
     * 查询新鲜时报列表
     */
    @SaCheckPermission("newtimes:newTimes:list")
    @GetMapping("/list")
    public TableDataInfo<KxNewTimesVo> list(KxNewTimesBo bo, PageQuery pageQuery) {
        bo.setStorageIds(getLoginUser().getStoragePermission());
        return iKxNewTimesService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出新鲜时报列表
     */
    @SaCheckPermission("newtimes:newTimes:export")
    @Log(title = "新鲜时报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(KxNewTimesBo bo, HttpServletResponse response) {
        List<KxNewTimesVo> list = iKxNewTimesService.queryList(bo);
        ExcelUtil.exportExcel(list, "新鲜时报", KxNewTimesVo.class, response);
    }

    /**
     * 获取新鲜时报详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("newtimes:newTimes:query")
    @GetMapping("/{id}")
    public R<KxNewTimesVo> getInfo(@NotNull(message = "主键不能为空")
                                   @PathVariable Long id) {
        return R.ok(iKxNewTimesService.queryById(id));
    }

    /**
     * 新增新鲜时报
     */
    @SaCheckPermission("newtimes:newTimes:add")
    @Log(title = "新鲜时报", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody KxNewTimesBo bo) {
        return toAjax(iKxNewTimesService.insertByBo(bo));
    }

    /**
     * 修改新鲜时报
     */
    @SaCheckPermission("newtimes:newTimes:edit")
    @Log(title = "新鲜时报", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody KxNewTimesBo bo) {
        return toAjax(iKxNewTimesService.updateByBo(bo));
    }

    /**
     * 删除新鲜时报
     *
     * @param ids 主键串
     */
    @SaCheckPermission("newtimes:newTimes:remove")
    @Log(title = "新鲜时报", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iKxNewTimesService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
